﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class HangXe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["AdminName"] != null)
            {
                DisplayList();
            }
            else
                Response.Redirect("Default.aspx");            
        }
    }
    protected void DisplayList()
    {
        string sql = "select * from hangxe";
        DataTable dt = DataBase.GetDataTable(sql);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Multiview1.ActiveViewIndex = 0;
    }
    protected void DisplayListEdit(string MaHang)
    {
        string sql = "select * from hangxe where MaHangXe='" + MaHang + "'";
        DataTable dt = DataBase.GetDataTable(sql);
        foreach (DataRow dtr in dt.Rows)
        {
            TextBox1.Text = dtr["MaHangXe"].ToString();
            TextBox2.Text = dtr["TenHang"].ToString();
        }
        Multiview1.ActiveViewIndex = 1;
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;
        string maHang = GridView1.Rows[index].Cells[2].Text;
        string sql = "delete from HangXe where MaHangXe='" + maHang + "'";
        try
        {
            DataBase.ExecuteNonQuery(sql);
            this.DisplayList();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    string maHangE;
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            maHangE = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
            this.DisplayListEdit(maHangE);
            TextBox1.ReadOnly = true;
            Button2.Text = "Update";
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //them
        if (Button2.Text.Equals("Add"))
        {
            string sql = string.Format(@"
                                        INSERT INTO HangXe
                                        (
                                            MaHangXe,
                                            TenHang
                                        )
                                        Values
                                        (
                                            '{0}','{1}'
                                        )
                                        ", TextBox1.Text, TextBox2.Text);
            try
            {
                DataBase.ExecuteNonQuery(sql);
                this.DisplayList();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
        else
        {
            string sql = "update hangxe set TenHang='" + TextBox2.Text + "' where MaHangXe='" + TextBox1.Text + "'";
            try
            {
                DataBase.ExecuteNonQuery(sql);
                this.DisplayList();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        this.DisplayList();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Multiview1.ActiveViewIndex = 1;
        TextBox1.ReadOnly = false;
        Button2.Text = "Add";
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.DisplayList();
    }
}
